package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DrgsServiceApplication;
import com.dhcc.DRGsHosp.application.MedInsCompleteApplication;
@Named
@Transactional
public class MedInsCompleteApplicationImpl implements MedInsCompleteApplication{

	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	@Autowired
	private DrgsServiceApplication drgsServiceApplication;
	/**
	 * 
	 *  @Description：查询医保主键问题病例
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.application.MedInsCompleteApplication#medInsQuestionsQuery()
	 */
	@Override 
	public List<Map<String,Object>> medInsQuestionsQuery(String dateFrom,String dateTo){
		String sql=" SELECT M.PARID,M.MR_NO,D.DIC_DESC,M.FUND_PAY,M.PRIVATE_PAY,M.PRIVATE_PAY1,M.PRIVATE_PAY2,M.RED_MARK, \n";
		sql+=" TO_CHAR(M.OUT_DATE,'YYYY-MM-DD')OUT_DATE,TO_CHAR(M.IN_DATE,'YYYY-MM-DD')IN_DATE, \n";
		sql+=" M.MEDICAL_CATEGORY,M.OUT_TYPE,M.REFUSE_FEE,M.REFUSE_REASON  \n";
		sql+=" FROM DRGS.T_MEDINS M LEFT JOIN DRGS.MR_DICTIONARYDATA D ON M.HI_TYPE=D.DIC_VALUE \n";
		sql+=" WHERE M.IS_ABNORMAL='1' AND D.DIC_CODE='MED_INS_TYPE' AND TO_CHAR(M.OUT_DATE,'YYYY-MM-DD') BETWEEN '"+dateFrom+"' AND '"+dateTo+"' \n";
		List<Map<String,Object>> listmap=jdbcTemplateObj.queryForList(sql.toString());
		return listmap;
	}
	/**
	 * 
	 *  @Description：更新主键和异常标志字段
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.application.MedInsCompleteApplication#updateMedinsQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public String updateMedinsQuery(String id,String mrNo, String outDate, String inDate){
			String sql="UPDATE DRGS.T_MEDINS M SET M.OUT_DATE=TO_DATE('"+outDate+"','YYYY-MM-DD'),M.IN_DATE=TO_DATE('"+inDate+"','YYYY-MM-DD') WHERE M.PARID='"+id+"'";
			jdbcTemplateObj.execute(sql);
			Map<String,String> mapId = drgsServiceApplication.findPmainindex(mrNo,inDate,outDate);
			String sqlt="UPDATE DRGS.T_MEDINS M SET M.PARID='"+mapId.get("mr_id")+"',M.IS_ABNORMAL='"+mapId.get("flag")+"' WHERE M.PARID='"+id+"'  and M.IN_DATE=TO_DATE('"+inDate+"','YYYY-MM-DD') and M.OUT_DATE=TO_DATE('"+outDate+"','YYYY-MM-DD') ";
			jdbcTemplateObj.execute(sqlt);	
		return mapId.get("flag");
	}
}
